import React from 'react'
import { Table,Button } from 'antd'
import { routerRedux } from 'dva/router'
import styles from './List.less'

const List = ({ ...tableProps }) => {
  const {productList}=tableProps
  const {suiteList}=tableProps
  const goto=tableProps.goto

  let productName=function(id){
    for(let i=0;i<productList.length;i++){
      if(productList[i].pk==id){
        return productList[i].fields.name
      }
    }
  }

  let suiteName=(arr)=>{
    let suiteNames=[]
    arr=arr.split(',')
    arr.map(item=>{
      for(let i=0;i<suiteList.length;i++){
        if(suiteList[i].pk==item){
          suiteNames.push(suiteList[i].fields.name)
        }
      }
    })
    return String(suiteNames)
  }
  const gotoHandler=(record)=>{
    let plan=record;
    plan.productName=productName(record.fields.product_name)
    // plan.suiteName=suiteName(record.fields.suite_names)
    plan.suiteName=record.fields.suite_names
    plan.name=record.fields.name
    plan.description=record.fields.description
    plan.productId=record.fields.product_name
    sessionStorage.setItem('currentPlan',JSON.stringify(plan))
    goto()
  }
  const columns = [
    {
      title: '测试计划ID',
      dataIndex: 'pk',
      key: 'pk',
      width: 80,
    },{
      title: '测试计划名称',
      dataIndex: 'fields.name',
      key: 'name',
      render: (text,record) => record.fields.name,
    },{
      title: '产品名称',
      dataIndex: 'fields.product_name',
      key: 'product_name',
      render: (text,record) => productName(record.fields.product_name),
    },{
      title: '套件名称',
      dataIndex: 'fields.suite_names',
      key: 'suite_names',
      render: (text,record) => suiteName(record.fields.suite_names),
    },{
      title: '描述',
      dataIndex: 'fields.description',
      key: 'description',
      render: (text,record) => record.fields.description,
    },
    {
      title: '操作',
      key: 'operation',
      width: 100,
      render: (text, record) => <Button onClick={e=>{gotoHandler(record,e)}}>执行计划</Button>,
    },
  ]

  return (
    <div>
      <Table
        {...tableProps}
        bordered
        columns={columns}
        simple
        className={styles.table}
        rowKey={record => record.pk}
        rowSelection={null}
      />
    </div>
  )
}

export default List
